package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.TripMultipleListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.RequisitionDetail;
import com.isunland.managesystem.entity.RequisitionTripOriginal;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by lenovo on 2016/5/12.
 */
public class TripMultipleListFragment extends BaseListFragment {
  protected static final String EXTRA_TRIP_NAMES =
      "com.isunland.managesystem.ui.TripMultipleListFragment.EXTRA_TRIP_NAMES";
  protected static final String EXTRA_TRIP_IDS =
      "com.isunland.managesystem.ui.TripMultipleListFragment.EXTRA_TRIP_IDS";
  private TripMultipleListAdapter mAdapter;
  private CurrentUser mUser;
  private String tripIds = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle("差旅单列表");
    mUser = CurrentUser.newInstance(getActivity());
    tripIds = (String) getArguments().getSerializable(EXTRA_TRIP_IDS);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.ULR_TRIP_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("regStaffId", mUser.getJobNumber());
    params.put("memberCode", mUser.getMemberCode());
    params.put("dataStatus", "checkPass");
    params.put("orderField", "REG_DATE");
    params.put("orderSeq", "desc");
    params.put("page", "1");
    params.put("rows", "1000");
    //查看已关联差旅单列表
    if (tripIds != null) {
      params.put("ids", tripIds);
    }
    return params;
  }

  @Override public void resolveResponse(String json) {
    RequisitionTripOriginal original = new Gson().fromJson(json, RequisitionTripOriginal.class);
    ArrayList<RequisitionDetail> dataList = (ArrayList<RequisitionDetail>) original.getRows();
    setAdapter(dataList);
  }

  private void setAdapter(ArrayList<RequisitionDetail> dataList) {
    if (dataList == null) {
      dataList = new ArrayList<RequisitionDetail>();
    }
    if (mAdapter == null) {
      mAdapter = new TripMultipleListAdapter(getActivity(), dataList, tripIds);
      setListAdapter(mAdapter);
      return;
    }
    mAdapter.clear();
    mAdapter.addAll(dataList);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (tripIds == null) {
      inflater.inflate(R.menu.menu_confirm, menu);
    } else {
      super.onCreateOptionsMenu(menu, inflater);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //                if (NavUtils.getParentActivityName(getActivity())!=null){
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        return true;
      //强多选的产品拼接
      case R.id.menu_item_confirm:
        if (mAdapter != null) {
          getTripString();
        }
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void getTripString() {
    //选择后要清空之前的数据.用新建对象的方式清空.
    ArrayList<String> codes = new ArrayList<String>();
    ArrayList<String> names = new ArrayList<String>();
    for (int i = 0; i < mAdapter.getCount(); i++) {
      LogUtil.i("mAdapterisChecked=" + i + mAdapter.isCheckedItem(i));
      if (mAdapter.isCheckedItem(i) == true) {
        LogUtil.i("选中的类型名==" + mAdapter.getItem(i).getBtripReason());
        codes.add(mAdapter.getItem(i).getId());
        names.add(mAdapter.getItem(i).getBtripReason());
      }
    }
    StringBuilder codeBuilder = new StringBuilder();
    StringBuilder nameBuilder = new StringBuilder();

    for (int i = codes.size() - 1; i >= 0; i--) {

      //            if (codeBuilder.length() > 0) {
      //                codeBuilder.append(",").append("'"+codes.get(i)+"'");
      //            } else {
      //                codeBuilder.append("'"+codes.get(i)+"'");
      //            }
      if (codeBuilder.length() > 0) {
        codeBuilder.append(",").append(codes.get(i));
      } else {
        codeBuilder.append(codes.get(i));
      }
      if (nameBuilder.length() > 0) {
        nameBuilder.append("--").append(names.get(i));
      } else {
        nameBuilder.append(names.get(i));
      }
    }

    Intent i = new Intent();
    i.putExtra(EXTRA_TRIP_IDS, codeBuilder.toString());
    i.putExtra(EXTRA_TRIP_NAMES, nameBuilder.toString());

    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();

    LogUtil.i("codeBuilder===" + codeBuilder);
    LogUtil.i("nameBuilder===" + nameBuilder);
  }

  public static TripMultipleListFragment newInstance(String tripIds) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_TRIP_IDS, tripIds);
    TripMultipleListFragment mFragment = new TripMultipleListFragment();
    mFragment.setArguments(bundle);
    return mFragment;
  }
}
